#include "RLSNetwork.h"

#include "../../inc/tinyxml2.h"

CRLSNetwork::CRLSNetwork(int inputDimension, int outputDimension) :
	m_InputDimension(inputDimension),
	m_OutputDimension(outputDimension)
{
	for (int i = 0; i < outputDimension; i++)
	{
		auto rlsNeuron = make_shared<CRLSNeuron>(inputDimension);

		this->m_RLSNeuron.push_back(rlsNeuron);
	}
}

bool CRLSNetwork::Save(const char * szFileName)
{
	tinyxml2::XMLDocument xmlDoc;

	xmlDoc.InsertFirstChild(xmlDoc.NewDeclaration());

	auto rootElement = xmlDoc.NewElement("RLS-Network");
	xmlDoc.InsertEndChild(rootElement);

	rootElement->SetAttribute("InputDimension", this->m_InputDimension);
	rootElement->SetAttribute("OutputDimension", this->m_OutputDimension);

	for (auto it_neuron = this->m_RLSNeuron.begin();
		it_neuron != this->m_RLSNeuron.end();
		++it_neuron)
	{
		CRLSNeuron & rlsNeuron = *(*it_neuron);

		auto neuron = xmlDoc.NewElement("RLS-Neuron");
		neuron->SetText(rlsNeuron.GetWeight().ToString().c_str());

		rootElement->InsertEndChild(neuron);
	}

	xmlDoc.SaveFile(szFileName);

	return true;
}

bool CRLSNetwork::Load(const char * szFileName)
{
	return true;
}

Vector CRLSNetwork::Execute(Vector InputData)
{
	int outputDim = this->m_RLSNeuron.size();

	Vector vecOutput(outputDim);

	int neuronIndex = 0;
	for (auto it_neuron = this->m_RLSNeuron.begin(); it_neuron != this->m_RLSNeuron.end(); ++it_neuron, ++neuronIndex)
	{
		vecOutput[neuronIndex] = (*it_neuron)->Execute(InputData);
	}

	return vecOutput;
}

int CRLSNetwork::GetInputDimension()
{
	return this->m_InputDimension;
}

int CRLSNetwork::GetOutputDimension()
{
	return this->m_OutputDimension;
}